package com.elong.android.specialhouse.activity;

import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.elong.android.specialhouse.ApartmentAPI;
import com.elong.android.specialhouse.R;
import com.elong.android.specialhouse.account.Account;
import com.elong.android.specialhouse.adapter.common.PowerAdapter;
import com.elong.android.specialhouse.base.BaseVolleyActivity;
import com.elong.android.specialhouse.entity.PushMessageVo;
import com.elong.android.specialhouse.entity.response.GetPushMessageByTypeResp;
import com.elong.android.specialhouse.request.GetMessageListByTypeReq;
import com.elong.android.specialhouse.utils.ToastUtils;
import com.elong.framework.netmid.ElongRequest;
import com.elong.framework.netmid.response.IResponse;
import com.elong.framework.netmid.response.StringResponse;

import java.util.List;

/**
 * 展示所有系统消息的界面的适配页面
 * 
 * @author boyan
 * 
 */
public abstract class SystemMessageListActivity extends
		BaseVolleyActivity<IResponse<?>> {
	/**
	 * 信息列表
	 */
	protected ListView mListView;
	protected PowerAdapter<PushMessageVo> mAdapter;
	protected List<PushMessageVo> messageList;
	protected GetMessageListByTypeReq req;

	@Override
	protected void initContentView() {
		setContentView(R.layout.act_systemmessagelist);
		mListView = (ListView) findViewById(R.id.message_list);
		setCommonHead();
		setData();
	}

	/**
	 * 设置标题
	 */
	abstract protected void setCommonHead();

	/**
	 * 设置后去信息的类型
	 */
	abstract protected void setDataType();

	/**
	 * 填充数据
	 */
	private void setData() {
		req = new GetMessageListByTypeReq();
		req.userId = Long.valueOf(Account.getInstance().getUserId());
		setDataType();
		req.pageIndex = 1;
		req.pageSize = 60000;
		requestHttp(req, ApartmentAPI.getMessageListByType,
				StringResponse.class, true);
	}

	/**
	 * 设置数据的adapter
	 * 
	 */
	abstract protected void setUpMsgList();

	@Override
	public void onTaskPost(ElongRequest request, IResponse<?> t) {
		// TODO Auto-generated method stub
		super.onTaskPost(request, t);
		JSONObject result = (JSONObject) JSONObject.parse(((StringResponse) t)
				.getContent());
		// System.out.println(result.toJSONString());
		// if(result != null){
		switch (((ApartmentAPI) request.getRequestOption().getHusky())) {
		case getMessageListByType:
			if (result.getBoolean("IsError")) {
				String message = result.getString("ErrorMessage");
				ToastUtils.show(this, message);
			} else {
				GetPushMessageByTypeResp message = JSON.parseObject(
						result.toJSONString(), GetPushMessageByTypeResp.class);
				messageList = message.messageVos;
				setUpMsgList();
				// mAdapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
